package com.ruoyi.db.tool.domain;

import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.ArrayUtils;
import com.ruoyi.common.constant.GenConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.BaseEntity;

/**
 * 业务表 gen_table
 *
 * @author ruoyi
 */
@ApiModel("代码生成业务表实体类")
@Data
@EqualsAndHashCode(callSuper = true)
public class GenTable extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("编号")
    private Long tableId;

    @ApiModelProperty("表名称")
    @NotBlank(message = "表名称不能为空")
    private String tableName;

    @ApiModelProperty("表描述")
    @NotBlank(message = "表描述不能为空")
    private String tableComment;

    @ApiModelProperty("实体类名称(首字母大写)")
    @NotBlank(message = "实体类名称不能为空")
    private String className;

    @ApiModelProperty("使用的模板（crud单表操作 tree树表操作）")
    private String tplCategory;

    @ApiModelProperty("生成包路径")
    @NotBlank(message = "生成包路径不能为空")
    private String packageName;

    @ApiModelProperty("生成模块名")
    @NotBlank(message = "生成模块名不能为空")
    private String moduleName;

    @ApiModelProperty("生成业务名")
    @NotBlank(message = "生成业务名不能为空")
    private String businessName;

    @ApiModelProperty("生成功能名")
    @NotBlank(message = "生成功能名不能为空")
    private String functionName;

    @ApiModelProperty("生成作者")
    @NotBlank(message = "作者不能为空")
    private String functionAuthor;

    @ApiModelProperty("主键信息")
    private GenTableColumn pkColumn;

    @ApiModelProperty("表列信息")
    @Valid
    private List<GenTableColumn> columns;

    @ApiModelProperty("其它生成选项")
    private String options;

    @ApiModelProperty("树编码字段")
    private String treeCode;

    @ApiModelProperty("树父编码字段")
    private String treeParentCode;

    @ApiModelProperty("树名称字段")
    private String treeName;

    /**  判断是否为树表（增删改查）  **/
    public boolean isTree()
    {
        return isTree(this.tplCategory);
    }

    public static boolean isTree(String tplCategory)
    {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_TREE, tplCategory);
    }

    /**  判断是否为单表（增删改查）  **/
    public boolean isCrud()
    {
        return isCrud(this.tplCategory);
    }

    public static boolean isCrud(String tplCategory)
    {
        return tplCategory != null && StringUtils.equals(GenConstants.TPL_CRUD, tplCategory);
    }

    /**  判断是否为基类中的字段  **/
    public boolean isSuperColumn(String javaField)
    {
        return isSuperColumn(this.tplCategory, javaField);
    }

    public static boolean isSuperColumn(String tplCategory, String javaField)
    {
        if (isTree(tplCategory))
        {
            return StringUtils.equalsAnyIgnoreCase(javaField,
                    ArrayUtils.addAll(GenConstants.TREE_ENTITY, GenConstants.BASE_ENTITY));
        }
        return StringUtils.equalsAnyIgnoreCase(javaField, GenConstants.BASE_ENTITY);
    }
}
